﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Handrise.CDCable.Mobile.Business
{
    /// <summary>

    ///  实现修改本地时间的功能
    /// </summary>
    public class SyncSystemTime
    {
        /// <summary>
        /// 根据传入的时间信息，修改本地时间
        /// </summary>
        /// <param name="serviceTime">传入的时间信息</param>
        public void SetLocalSystemTime(DateTime datetime)
        {
            try
            {
                string serviceTime = datetime.ToString("yyyyMMddHHmmss");
                // 系统时间结构
                SYSTEMTIME systime = new SYSTEMTIME();
                // 截取年信息
                systime.wYear = (ushort)(Convert.ToUInt32(serviceTime.Substring(0, 4)));
                // 截取月信息
                systime.wMonth = (ushort)(Convert.ToUInt32(serviceTime.Substring(4, 2)));
                // 截取日信息
                systime.wDay = (ushort)(Convert.ToUInt32(serviceTime.Substring(6, 2)));
                // 截取小时信息
                systime.wHour = (ushort)(Convert.ToUInt32(serviceTime.Substring(8, 2)));
                if (systime.wHour < 8)
                {
                    systime.wDay = (ushort)(systime.wDay - 1);
                    if (systime.wDay <= 0)
                    {
                        systime.wMonth = (ushort)(systime.wMonth - 1);
                        if (systime.wMonth <= 0)
                        {
                            systime.wMonth = 12;

                            systime.wYear = (ushort)(systime.wYear - 1);

                        }

                        switch (systime.wMonth)
                        {
                            case 1:
                            case 3:
                            case 5:
                            case 7:
                            case 8:
                            case 10:
                            case 12:
                                systime.wDay = 31;
                                break;
                            case 2:
                                if ((systime.wYear / 4 == 0) ||
                                    ((systime.wYear / 100 == 0) && (systime.wYear / 400 == 0)))
                                {
                                    systime.wDay = 29;
                                }
                                else
                                {

                                    systime.wDay = 28;
                                }
                                break;
                            default:
                                systime.wDay = 30;
                                break;
                        }

                    }
                }
                // 将时间转为东八区时间
                systime.wHour = (ushort)((systime.wHour + 24 - 8) % 24);
                // 截取分钟信息
                systime.wMinute = (ushort)(Convert.ToUInt32(serviceTime.Substring(10, 2)));
                // 截取秒信息
                systime.wSecond = (ushort)(Convert.ToUInt32(serviceTime.Substring(12, 2)));
                // 调用系统方法，设置系统实际
                SetSystemTime(ref systime);
            }
            catch (Exception)
            {
            }
        }

        [DllImport("coredll.dll")]
        private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);

        /// <summary>
        /// 系统时间结构
        /// </summary>
        private struct SYSTEMTIME
        {
            // 年
            public ushort wYear;
            // 月
            public ushort wMonth;
            // 星期
            public ushort wDayOfWeek;
            // 日
            public ushort wDay;
            // 小时
            public ushort wHour;
            // 分钟
            public ushort wMinute;
            // 秒
            public ushort wSecond;
            // 毫秒
            public ushort wMilliseconds;
        }
    }
    // end of class
}
// end of namespace


